package ma.belsoft.syga.webapp.action;

import com.opensymphony.xwork2.Preparable;
import org.appfuse.service.GenericManager;

import ma.belsoft.syga.model.Marque;
import ma.belsoft.syga.model.Vehicule;
import ma.belsoft.syga.model.Ville;
import ma.belsoft.syga.service.MarqueManager;
import ma.belsoft.syga.service.VehiculeManager;
import ma.belsoft.syga.service.VilleManager;
import ma.belsoft.syga.webapp.action.BaseAction;

import java.util.ArrayList;
import java.util.List;

public class VehiculeAction extends BaseAction implements Preparable {
    private VehiculeManager vehiculeManager;
    private MarqueManager marqueManager;
	private List<Marque> marques = new ArrayList<Marque>();
	private VilleManager villeManager;
    private List<Ville> villes = new ArrayList<Ville>();
    private List vehicules;
    private Vehicule vehicule;
    private Long id;
    private String query;

	public void setVehiculeManager(VehiculeManager vehiculeManager) {
		this.vehiculeManager = vehiculeManager;
	}

	public void setMarqueManager(MarqueManager marqueManager) {
		this.marqueManager = marqueManager;
	}

	public List<Marque> getMarques() {
		return marqueManager.getAll();
	}

	public void setMarques(List<Marque> marques) {
		this.marques = marques;
	}

	public void setVilleManager(VilleManager villeManager) {
		this.villeManager = villeManager;
	}

	public List<Ville> getVilles() {
		return villeManager.getAll();
	}

	public void setVilles(List<Ville> villes) {
		this.villes = villes;
	}

	public List getVehicules() {
        return vehicules;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String vehiculeId = getRequest().getParameter("vehicule.id");
            if (vehiculeId != null && !vehiculeId.equals("")) {
                vehicule = vehiculeManager.get(new Long(vehiculeId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        vehicules = vehiculeManager.search(query, Vehicule.class);
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Vehicule getVehicule() {
        return vehicule;
    }

    public void setVehicule(Vehicule vehicule) {
        this.vehicule = vehicule;
    }

    public String delete() {
        vehiculeManager.remove(vehicule.getId());
        saveMessage(getText("vehicule.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            vehicule = vehiculeManager.get(id);
        } else {
            vehicule = new Vehicule();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (vehicule.getId() == null);

        vehiculeManager.save(vehicule);

        String key = (isNew) ? "vehicule.added" : "vehicule.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}